<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Enclosed
 *
 * @author agama
 */
class Enclosed extends Node
{

    const PROJECT_ENCLOSED = 0;
    const MILESTONE_ATTACHED = 1;
    const MILESTONE_TO_ATTACH = 2;
    const TINY_SIZE = 0;
    const MEDIUM_SIZE = 1;
    const BIG_SIZE = 2;

    protected $type;
    protected $collaborative;
    protected $active;
    protected $revision;
    protected $extension;
    protected $title;
    protected $url;
    protected $owner;
    protected $in_progress; //no es lo mismo upload o donwload

    /*
      protected $owner_name;
     */
    protected $resource_project;
    protected $resource_enclosed;
    protected $editable = false;
    protected $attachable = false;
    protected $levelable = false;
    public $mode = self::PROJECT_ENCLOSED;
    public $size = self::BIG_SIZE;

    function handleGET( Event $e )
    {
        //view, download, edit
        //si el objeto eestá en google docs lo bajaremos de allí
        //si no está en google docs lo bajaremos del servidor

        if ( $e->tag == 'view' ) {
            if ( empty( GSharedAPI::$instances[ 0 ] ) )
                    GSharedAPI::$instances[ 0 ] = new GShareAPI( GOOGLE_ACCOUNT,
                        MyLink::$default );

            $data = GSharedAPI::$instances[ 0 ]->isUploaded( 'enclosed_files/' . $this->project . '/' . $this->id . '.' . $this->revision . '.' . $this->extension );

            if ( empty( $data ) ) {
                list($object_id, $view_link, $edit_link, $export_link) = $data;
            } else {
                list($object_id, $view_link, $edit_link, $export_link) = GSharedAPI::$instances[ 0 ]->uploadNewFile( 'enclosed_files/' . $this->project . '/' . $this->id . '.' . $this->revision . '.' . $this->extension,
                        $this->title );
            }

            if ( !empty( $object_id ) && GSharedAPI::$instances[ 0 ]->share( $object_id,
                            GShareAPI::PERMISSION_READER,
                            $_SESSION[ 'google_user_account' ] ) ) {
                header( 'Location: ' . $view_link );
                exit();
            }
            else
                    self::output( 'enclosed_files/' . $this->project . '/' . $this->id . '.' . $this->revision . '.' . $this->extension,
                        false );

            //un header location a la URL de google docs
        }
        else if ( $e->tag == 'download' ) {

            if ( empty( GSharedAPI::$instances[ 0 ] ) )
                    GSharedAPI::$instances[ 0 ] = new GShareAPI( GOOGLE_ACCOUNT,
                        MyLink::$default );

            $data = GSharedAPI::$instances[ 0 ]->isUploaded( 'enclosed_files/' . $this->project . '/' . $this->id . '.' . $this->revision . '.' . $this->extension );

            if ( !empty( $data ) ) {

                list($object_id, $view_link, $edit_link, $export_link) = $data;

                $ext = GSharedAPI::$mime2export[ GSharedAPI::$ext2mime[ $this->extension ] ];

                $export_link.= '&exportFormat=' . $ext . '&format=' . $ext;
                header( 'Location: ' . $export_link );
                exit();
            }
            else
                    self::output( 'enclosed_files/' . $this->project . '/' . $this->id . '.' . $this->revision . '.' . $this->extension,
                        true );
        }
    }

}

?>
